<template>
	<view class="bg-white">
		<cu-custom bgColor="bg-white" :isBack="true">
			<block slot="content">水电费详情</block>
			<!-- <block slot="right">
				<button class="cu-btn bg-red shadow" @click="urging">催办</button>
			</block> -->
		</cu-custom>
		<view class="cu-bar bg-white solid-bottom">
			<view class="action">
				<text class="cuIcon-titles " style="color: #1E5EFF;"></text> 支付明细
			</view>
		</view>
		<view>
			<view class="flex">
				<view class="flex-sub padding-sm margin-xs radius">
					公司
				</view>
				<view class="flex-sub padding-sm margin-xs radius">
					{{dataObj.company_dictText}}
				</view>
			</view>
			<view class="flex">
				<view class="flex-sub padding-sm margin-xs radius">
					楼栋
				</view>
				<view class="flex-sub padding-sm margin-xs radius">
					{{dataObj.buildingId_dictText}}
				</view>
			</view>
			<view class="flex">
				<view class="flex-sub padding-sm margin-xs radius">
					楼层
				</view>
				<view class="flex-sub padding-sm margin-xs radius">
					{{dataObj.floorId_dictText}}
				</view>
			</view>
			<view class="flex">
				<view class="flex-sub padding-sm margin-xs radius">
					宿舍号
				</view>
				<view class="flex-sub padding-sm margin-xs radius">
					{{dataObj.roomCode}}
				</view>
			</view>
		</view>
		<view style="padding: 10px;">
			<text @click="addDetails"
				style="padding: 8rpx 20rpx;background-color: rgba(69, 145, 254, 1);border-radius: 10rpx;color:#FFFFFF;">
				新增
			</text>
		</view>
		<view v-for="(item, index) in dataObj.dormitoryWaterElectricityList" :key="index"
			class="solid-bottom margin-bottom-sm">
			<view class="flex">
				<view class="flex-sub padding-sm margin-xs radius">
					抄表日期
				</view>
				<view class="flex-sub padding-sm margin-xs radius">
					{{item.meterReadingTime.replaceAll('-','/')}}
				</view>
			</view>
			<view class="flex">
				<view class="flex-sub padding-sm margin-xs radius">
					阳台水表底数
				</view>
				<view class="flex-sub padding-sm margin-xs radius">
					{{item.balconyWaterBase}}
				</view>
			</view>
			<view class="flex">
				<view class="flex-sub padding-sm margin-xs radius">
					阳台水表读数
				</view>
				<view class="flex-sub padding-sm margin-xs radius">
					{{item.balconyWater}}
				</view>
			</view>
			<view class="flex">
				<view class="flex-sub padding-sm margin-xs radius">
					阳台水表实际用水（吨）
				</view>
				<view class="flex-sub padding-sm margin-xs radius">
					{{item.balconyWaterReality}}
				</view>
			</view>
			<view class="flex">
				<view class="flex-sub padding-sm margin-xs radius">
					阳台水单价（元/吨）
				</view>
				<view class="flex-sub padding-sm margin-xs radius">
					{{item.balconyWaterUnitPrice}}
				</view>
			</view>
			<view class="flex">
				<view class="flex-sub padding-sm margin-xs radius">
					厨房水表底数
				</view>
				<view class="flex-sub padding-sm margin-xs radius">
					{{item.kitchenWaterBase}}
				</view>
			</view>
			<view class="flex">
				<view class="flex-sub padding-sm margin-xs radius">
					厨房水表读数
				</view>
				<view class="flex-sub padding-sm margin-xs radius">
					{{item.kitchenWater}}
				</view>
			</view>
			<view class="flex">
				<view class="flex-sub padding-sm margin-xs radius">
					厨房实际用水（吨）
				</view>
				<view class="flex-sub padding-sm margin-xs radius">
					{{item.kitchenWaterReality}}
				</view>
			</view>
			<view class="flex">
				<view class="flex-sub padding-sm margin-xs radius">
					厨房水单价（元/吨）
				</view>
				<view class="flex-sub padding-sm margin-xs radius">
					{{item.kitchenWaterUnitPrice}}
				</view>
			</view>
			<view class="flex">
				<view class="flex-sub padding-sm margin-xs radius">
					热水水表底数
				</view>
				<view class="flex-sub padding-sm margin-xs radius">
					{{item.hotWaterBase}}
				</view>
			</view>
			<view class="flex">
				<view class="flex-sub padding-sm margin-xs radius">
					热水水表读数
				</view>
				<view class="flex-sub padding-sm margin-xs radius">
					{{item.hotWater}}
				</view>
			</view>
			<view class="flex">
				<view class="flex-sub padding-sm margin-xs radius">
					热水实际用水（吨）
				</view>
				<view class="flex-sub padding-sm margin-xs radius">
					{{item.hotWaterReality}}
				</view>
			</view>
			<view class="flex">
				<view class="flex-sub padding-sm margin-xs radius">
					热水水单价（元/吨）
				</view>
				<view class="flex-sub padding-sm margin-xs radius">
					{{item.hotWaterUnitPrice}}
				</view>
			</view>
			<view class="flex">
				<view class="flex-sub padding-sm margin-xs radius">
					电表读数
				</view>
				<view class="flex-sub padding-sm margin-xs radius">
					{{item.electricity}}
				</view>
			</view>
			<view class="flex">
				<view class="flex-sub padding-sm margin-xs radius">
					实际用电（度）
				</view>
				<view class="flex-sub padding-sm margin-xs radius">
					{{item.electricityReality}}
				</view>
			</view>
			<view class="flex">
				<view class="flex-sub padding-sm margin-xs radius">
					电单价（元/度）
				</view>
				<view class="flex-sub padding-sm margin-xs radius">
					{{item.electricityUnitPrice}}
				</view>
			</view>
			<view class="flex">
				<view class="flex-sub padding-sm margin-xs radius">
					水费合计(元)
				</view>
				<view class="flex-sub padding-sm margin-xs radius">
					{{item.waterPrice}}
				</view>
			</view>
			<view class="flex">
				<view class="flex-sub padding-sm margin-xs radius">
					电费合计(元)
				</view>
				<view class="flex-sub padding-sm margin-xs radius">
					{{item.electricityPrice}}
				</view>
			</view>
			<view class="flex">
				<view class="flex-sub padding-sm margin-xs radius">
					水电费合计(元)
				</view>
				<view class="flex-sub padding-sm margin-xs radius">
					{{item.total}}
				</view>
			</view>
			<view class="flex">
				<view class="flex-sub padding-sm margin-xs radius">
					创建日期
				</view>
				<view class="flex-sub padding-sm margin-xs radius">
					{{item.createTime}}
				</view>
			</view>
			<view class="flex">
				<view style="padding: 10px;">
					<text @click="selectBottom"
						style="padding: 8rpx 20rpx;background-color: rgba(69, 145, 254, 1);border-radius: 10rpx;color:#FFFFFF;">
						选择表底
					</text>
				</view>
			</view>
		</view>

		<uni-popup ref="addWindow" background-color="#fff" :mask-click="false">
			<scroll-view scroll-y style="height: 1100rpx;">
				<view
					style="background-color: #FFFFFF;padding: 11rpx 30rpx;width: 580rpx;height: 2300rpx;border-radius: 42rpx;">
					<view style="margin-top: 28rpx;margin-left: 15rpx;">
						<text class="submit-text">水电明细</text>
					</view>
					<view class="cu-form-group" style="background-color: #FAFAFA;margin-top: 40rpx;">
						<text>抄表时间：</text>
						<picker mode="date" :value="model.meterReadingTime" @change="DateChange">
							<view style="text-align:right;line-height: 15px;">
								{{model.meterReadingTime != '' ? model.meterReadingTime.replaceAll('-','/') : '请选择'}}
							</view>
						</picker>
					</view>
					<view class="cu-form-group" style="background: #FAFAFA;text-align:right;">
						<view class="title">阳台水表底数</view>
						<input name="input" v-model="model.balconyWaterBase"></input>
					</view>
					<view class="cu-form-group" style="background: #FAFAFA;text-align:right;">
						<view class="title">阳台水表读数</view>
						<input name="input" v-model="model.balconyWater"></input>
					</view>
					<view class="cu-form-group" style="background: #E2E2E2;text-align:right;">
						<view class="title">阳台实际用水</view>
						<input name="input" disabled v-model="model.balconyWaterReality"></input>
					</view>
					<view class="cu-form-group" style="background: #FAFAFA;text-align:right;">
						<view class="title">阳台水单价（元/吨）</view>
						<input name="input" v-model="model.balconyWaterUnitPrice"></input>
					</view>
					<view class="cu-form-group" style="background: #FAFAFA;text-align:right;">
						<view class="title">厨房水表底数</view>
						<input name="input" v-model="model.kitchenWaterBase"></input>
					</view>
					<view class="cu-form-group" style="background: #FAFAFA;text-align:right;">
						<view class="title">厨房水表读数</view>
						<input name="input" v-model="model.kitchenWater"></input>
					</view>
					<view class="cu-form-group" style="background: #E2E2E2;text-align:right;">
						<view class="title">厨房水表实际用水</view>
						<input name="input" disabled v-model="model.kitchenWaterReality"></input>
					</view>
					<view class="cu-form-group" style="background: #FAFAFA;text-align:right;">
						<view class="title">厨房水单价（元/吨）</view>
						<input name="input" v-model="model.kitchenWaterUnitPrice"></input>
					</view>
					<view class="cu-form-group" style="background: #FAFAFA;text-align:right;">
						<view class="title">热水水表底数</view>
						<input name="input" v-model="model.hotWaterBase"></input>
					</view>
					<view class="cu-form-group" style="background: #FAFAFA;text-align:right;">
						<view class="title">热水水表读数</view>
						<input name="input" v-model="model.hotWater"></input>
					</view>
					<view class="cu-form-group" style="background: #E2E2E2;text-align:right;">
						<view class="title">热水水表实际用水</view>
						<input name="input" disabled v-model="model.hotWaterReality"></input>
					</view>
					<view class="cu-form-group" style="background: #FAFAFA;text-align:right;">
						<view class="title">热水水单价（元/吨）</view>
						<input name="input" v-model="model.hotWaterUnitPrice"></input>
					</view>
					<view class="cu-form-group" style="background: #FAFAFA;text-align:right;">
						<view class="title">电表底数</view>
						<input name="input" v-model="model.electricityBase" @input="totalWater"></input>
					</view>
					<view class="cu-form-group" style="background: #FAFAFA;text-align:right;">
						<view class="title">电表读数</view>
						<input name="input" v-model="model.electricity"></input>
					</view>
					<view class="cu-form-group" style="background: #E2E2E2;text-align:right;">
						<view class="title">实际用电（度）</view>
						<input name="input" disabled v-model="model.electricityReality" @input="totalElectric"></input>
					</view>
					<view class="cu-form-group" style="background: #FAFAFA;text-align:right;">
						<view class="title">电单价（元/度）</view>
						<input name="input" v-model="model.electricityUnitPrice" @input="totalElectric"></input>
					</view>
					<view class="cu-form-group" style="background: #E2E2E2;text-align:right;">
						<view class="title">水费合计(元)</view>
						<input disabled name="input" disabled v-model="model.waterPrice"></input>
					</view>
					<view class="cu-form-group" style="background: #E2E2E2;text-align:right;">
						<view class="title">电费合计(元)</view>
						<input disabled name="input" disabled v-model="model.electricityPrice"></input>
					</view>
					<view class="cu-form-group" style="background: #E2E2E2;text-align:right;">
						<view class="title">水电费合计(元)</view>
						<input disabled name="input" disabled v-model="model.total"></input>
					</view>
					<button style="background-color: rgba(217, 217, 217, 1);
			        				color: rgba(86, 85, 85, 1); margin-top: 30rpx;padding: 2rpx 85rpx; display: inline-block; "
						@click="rejectCancel">取消</button>
					<button style="background-color: rgba(69, 145, 254, 1);
			        	color: rgba(255, 255, 255, 1);margin-left:34rpx; display: inline-block;padding: 2rpx 85rpx; "
						@click="rejectSure">确定</button>
				</view>
			</scroll-view>

		</uni-popup>
	</view>
</template>

<script>
	import moment from 'moment';
	export default {
		data() {
			return {
				id: Math.floor(Math.random() * 10000000000000000000) + Date.now(),
				dataObj: {},
				editId: '',
				model: {
					meterReadingTime: '',
					water: 0,
					waterReality: 0,
					waterUnitPrice: 0,
					electricity: 0,
					electricityReality: 0,
					electricityUnitPrice: 0,
					waterPrice: 0,
					electricityPrice: 0,
					total: 0,
					balconyWater: 0,
					balconyWaterBase: 0,
					balconyWaterReality: 0,
					balconyWaterUnitPrice: 0,
					hotWater: 0,
					hotWaterBase: 0,
					hotWaterReality: 0,
					hotWaterUnitPrice: 0,
					kitchenWater: 0,
					kitchenWaterBase: 0,
					kitchenWaterReality: 0,
					kitchenWaterUnitPrice: 0,
					electricity: 0,
					electricityBase: 0,
					electricityPrice: 0,
					electricityReality: 0,
					electricityUnitPrice: 0,
				},
			}
		},
		mounted() {

		},
		watch: {
			// 监听阳台水表底数和水表读数的变化
			'model.balconyWaterBase': function(newVal, oldVal) {
				this.calculateBalconyWaterReality();
			},
			'model.balconyWater': function(newVal, oldVal) {
				this.calculateBalconyWaterReality();
			},
			// 监听阳台水表底数和水表读数的变化
			'model.hotWaterBase': function(newVal, oldVal) {
				this.calculateHotWaterReality();
			},
			'model.hotWater': function(newVal, oldVal) {
				this.calculateHotWaterReality();
			},
			// 监听阳台水表底数和水表读数的变化
			'model.kitchenWaterBase': function(newVal, oldVal) {
				this.calculateKitchenWaterReality();
			},
			'model.kitchenWater': function(newVal, oldVal) {
				this.calculateKitchenWaterReality();
			},
			// 监听阳台水表底数和水表读数的变化
			'model.electricityBase': function(newVal, oldVal) {
				this.calculateElectricityReality();
			},
			'model.electricity': function(newVal, oldVal) {
				this.calculateElectricityReality();
			},
			'model.balconyWaterReality': function(newVal, oldVal) {
				this.calculateTotalWater();
			},
			'model.balconyWaterUnitPrice': function(newVal, oldVal) {
				this.calculateTotalWater();
			},
			'model.kitchenWaterReality': function(newVal, oldVal) {
				this.calculateTotalWater();
			},
			'model.kitchenWaterUnitPrice': function(newVal, oldVal) {
				this.calculateTotalWater();
			},
			'model.hotWaterReality': function(newVal, oldVal) {
				this.calculateTotalWater();
			},
			'model.hotWaterUnitPrice': function(newVal, oldVal) {
				this.calculateTotalWater();
			},
		},
		computed: {

		},
		onLoad: function(options) {
			if (options.editId) {
				this.editId = options.editId;
				this.getDetail()
			}
		},
		methods: {
			calculateWaterTotal() {
				// 触发计算水费合计
				this.$nextTick(() => {
					this.waterTotal;
				});
			},
			getDetail() {
				this.$http.get(
						"/crm_dormitory_room/crmDormitoryRoom/queryDetail/" + this.editId)
					.then(res => {
						if (res.data.success) {
							this.dataObj = res.data.result
						}
					})
			},
			DateChange(e) {
				this.model.meterReadingTime = e.detail.value
			},
			addDetails() {
				this.$refs.addWindow.open('center')
			},
			totalWater() {
				if (parseFloat(this.model.waterReality) && parseFloat(this.model.waterReality)) {
					this.model.waterPrice = parseFloat(this.model.waterReality) * parseFloat(this.model.waterUnitPrice)
					this.total()
				}
			},
			totalElectric() {
				if (parseFloat(this.model.electricityReality) && parseFloat(this.model.electricityReality)) {
					this.model.electricityPrice = parseFloat(this.model.electricityReality) * parseFloat(this.model
						.electricityUnitPrice)
					this.total()
				}
			},
			total() {
				this.model.total = parseFloat(this.model.waterPrice) + parseFloat(this.model.electricityPrice)
			},
			rejectSure() {
				this.$http.post('/crm_dormitory_water_electricity/crmDormitoryWaterElectricity/add', {
						"water": this.model.water,
						"waterReality": this.model.waterReality,
						"waterUnitPrice": this.model.waterUnitPrice,
						"waterPrice": this.model.waterPrice,
						"total": this.model.total,
						"company": this.dataObj.company,
						"buildingId": this.dataObj.buildingId,
						"floorId": this.dataObj.floorId,
						"roomCode": this.dataObj.roomCode,
						"meterReadingTime": this.model.meterReadingTime,
						"id": this.id,
						'balconyWater': this.model.balconyWater,
						'balconyWaterBase': this.model.balconyWaterBase,
						'balconyWaterReality': this.model.balconyWaterReality,
						'balconyWaterUnitPrice': this.model.balconyWaterUnitPrice,
						'hotWater': this.model.hotWater,
						'hotWaterBase': this.model.hotWaterBase,
						'hotWaterReality': this.model.hotWaterReality,
						'hotWaterUnitPrice': this.model.hotWaterUnitPrice,
						'kitchenWater': this.model.kitchenWater,
						'kitchenWaterBase': this.model.kitchenWaterBase,
						'kitchenWaterReality': this.model.kitchenWaterReality,
						'kitchenWaterUnitPrice': this.model.kitchenWaterUnitPrice,
						'electricity': this.model.electricity,
						'electricityBase': this.model.electricityBase,
						'electricityPrice': this.model.electricityPrice,
						'electricityReality': this.model.electricityReality,
						'electricityUnitPrice': this.model.electricityUnitPrice,
					})
					.then(res => {
						if (res.data.success) {
							this.getDetail()
							this.$refs.addWindow.close()
						} else {
							this.$tip.toast(res.data.message)
						}
					}).catch(() => {
						this.$tip.loaded();
						this.$tip.toast('提交失败')
					});
			},
			rejectCancel() {
				this.$refs.addWindow.close()
			},
			selectBottom() {
				this.$refs.addWindow.open('center')
			},
			calculateBalconyWaterReality() {
				const balconyWaterBase = parseFloat(this.model.balconyWaterBase);
				const balconyWater = parseFloat(this.model.balconyWater);
				this.model.balconyWaterReality = balconyWaterBase + balconyWater;
			},
			calculateHotWaterReality() {
				const hotWaterBase = parseFloat(this.model.hotWaterBase);
				const hotWater = parseFloat(this.model.hotWater);
				this.model.hotWaterReality = hotWaterBase + hotWater;
			},
			calculateKitchenWaterReality() {
				const kitchenWaterBase = parseFloat(this.model.kitchenWaterBase);
				const kitchenWater = parseFloat(this.model.kitchenWater);
				this.model.kitchenWaterReality = kitchenWaterBase + kitchenWater;
			},
			calculateElectricityReality() {
				const electricityBase = parseFloat(this.model.electricityBase);
				const electricity = parseFloat(this.model.electricity);
				this.model.electricityReality = electricityBase + electricity;
			},
			calculateTotalWater(){
				this.totalWater()
			},
			totalWater() {
				this.model.waterPrice =
					this.model.balconyWaterReality * this.model.balconyWaterUnitPrice +
					this.model.kitchenWaterReality * this.model.kitchenWaterUnitPrice +
					this.model.hotWaterReality * this.model.hotWaterUnitPrice
				this.total()
			},
		}
	}
</script>

<style lang="scss" scoped>
	image {
		width: 100%;
		height: 100%;
	}

	.blackList {
		height: 100%;
		overflow: hidden;
	}

	.page-search {
		padding-left: 34rpx;
		margin-bottom: 12rpx;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}

	.search {
		width: 680rpx;
		height: 86rpx;
		background-color: rgba(249, 249, 249, 1);
		border-radius: 60rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
		padding-left: 24rpx;
		padding-right: 24rpx;

		.search-left {
			width: 28rpx;
			height: 28rpx;
			display: flex;
			justify-content: center;
			align-items: center;
		}

		.search-center {
			margin-left: 22rpx;
		}

		.search-placeholder {
			font-size: 28rpx;
			font-weight: 400;
			color: #7C7C7C;
		}


	}

	.filter-btn {
		width: 40rpx;
		height: 40rpx;
		margin-right: 42rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.page-content-main {
		height: calc(100% - 12rpx - 86rpx);
		border-top-left-radius: 64rpx;
		border-top-right-radius: 64rpx;
		padding: 26rpx 34rpx 0 34rpx
	}

	.select-box {
		display: flex;
		flex-direction: row;
		justify-content: flex-start;

	}

	.select {
		width: 160rpx;
		height: 74rpx;
		border-radius: 60rpx;
		background-color: #4591FE;
		margin-bottom: 20rpx;
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-items: center;
		margin-right: 48rpx;
		position: relative;

		.select-value {
			font-size: 28rpx;
			font-weight: 400;
			color: #FFFFFF;
			margin-right: 18rpx;
		}

		.select-icon {
			width: 22rpx;
			height: 22rpx;
			display: flex;
			justify-content: center;
			align-items: center;
			position: absolute;
			right: 20rpx;
			top: 25rpx;
		}
	}

	.list {
		height: calc(100% - 74rpx - 20rpx);
	}

	.list-item {
		border-radius: 28rpx;
		background-color: #FFFFFF;
		padding: 26rpx 24rpx 28rpx 20rpx;
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		margin-bottom: 26rpx;
		position: relative;

		.list-item-leftCenter {
			display: flex;
			flex-direction: row;
		}

		.list-item-left {
			width: 86rpx;
			height: 86rpx;
			margin-right: 26rpx;
			display: flex;
			justify-content: center;
			align-items: center;
		}

		.list-item-center {
			.list-item-row {
				display: flex;
				flex-direction: row;
				margin-bottom: 20rpx;

				.list-item-name {
					color: #101317;
					font-size: 32rpx;
					margin-right: 16rpx;
				}

				.list-item-sex {
					width: 106rpx;
					height: 42rpx;
					border-top-right-radius: 40rpx;
					border-bottom-right-radius: 40rpx;
					border-bottom-left-radius: 20rpx;
					background-color: rgba(234, 171, 78, 0.35);
					line-height: 42rpx;
					text-align: center;
					color: rgba(255, 173, 4, 1);
					font-size: 24rpx;
				}

				.list-item-row-icon {
					width: 38rpx;
					height: 38rpx;
					margin-right: 10rpx;
					display: flex;
					justify-content: center;
					align-items: center;
				}

				.list-item-row-title,
				.list-item-row-value {
					font-size: 24rpx;
					font-weight: 400;
					color: #101317;
					line-height: 38rpx;
				}

			}
		}

		.list-item-btn {
			width: 36rpx;
			height: 36rpx;
			margin-top: 56rpx;
			display: flex;
			justify-content: center;
			align-items: center;
		}

		.list-item-tab {
			width: 132rpx;
			height: 56rpx;
			text-align: center;
			line-height: 56rpx;
			background-color: rgba(69, 145, 254, 0.4);
			position: absolute;
			top: 0;
			right: 0;
			border-top-right-radius: 26rpx;
			border-bottom-left-radius: 20rpx;
		}

		.list-item-tab-text {
			font-size: 26rpx;
			color: rgba(69, 145, 254, 1)
		}
	}

	.nav-right-image {
		width: 80rpx;
		height: 80rpx;
		margin-left: 15px;
	}

	.cu-form-group {
		background-color: transparent;
	}

	::v-deep {
		.cu-form-group uni-picker .picker {
			color: #FFFFFF;
		}

		.cu-form-group uni-picker::after {
			opacity: 0;
		}
	}

	.textView {
		margin-left: 25px;
		font-size: 10px;
		margin-top: 5px;
	}

	.listText {
		width: 148rpx;
		height: 24rpx;
		font-size: 20rpx;
		font-family: PingFang SC, PingFang SC;
		font-weight: 400;
		color: #000000;
		line-height: 24rpx;
	}
</style>